package com.westcom.secritycheck;

import com.westcom.secritycheck.SecurityGate.SecurityGate;
import com.westcom.secritycheck.SecurityGate.impl.RandomSecrityGate;
import com.westcom.secritycheck.WaitingLine.IWaitingLine;
import com.westcom.secritycheck.WaitingLine.impl.LinkedListWaitingLine;
import com.westcom.secritycheck.bean.OperationException;
import com.westcom.secritycheck.bean.Passenger;
import com.westcom.secritycheck.bean.PassengerFactory;
import com.westcom.secritycheck.constants.Constants;
import com.westcom.secritycheck.constants.Utils;

import java.util.Random;

public class app
{
    private static SecurityGate gate = null;
    private static IWaitingLine waitingLine = null;
    private static void init()
    {
        //int waiting line
        waitingLine = new LinkedListWaitingLine();
        for(int i = 0;i<Constants.INITIALIZED_PASSENGER_IN_LINE;i++)
        {
            waitingLine.push(PassengerFactory.createRandomPassenger());
        }
        //init security gate
        try
        {
            gate = new RandomSecrityGate(Constants.CHECK_PERIOD,waitingLine);
        } catch (OperationException e)
        {
            Utils.printLog("create gate failed,msg:"+e.getMessage());
            e.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        init();
        if(null == gate)
        {
            Utils.printLog("gate is null,initialize failed,");
            return;
        }
        long simulated = 0;
        Random seed = new Random();
        gate.startCheck();
        while(simulated < Constants.SIMULATE_TIME)
        {
            try
            {
                long sleepS = Constants.SIMULATE_TIME - simulated > Constants.MAX_PERIOD_OF_NEW_PASSENGER ?
                seed.nextInt(Constants.MAX_PERIOD_OF_NEW_PASSENGER)+1:Constants.SIMULATE_TIME - simulated;
                Thread.sleep(sleepS*1000);
                Passenger p = PassengerFactory.createRandomPassenger();
                waitingLine.push(p);
                simulated +=sleepS;
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        gate.endCheck();
        Utils.printLog("end");
    }
}
